<template>
	<view>
		<view class="container" v-if="addressList.length">
			<radio-group @change="radioChange">
				<view class="panel" v-for="(item, index) in addressList" :key="index">
					<view class="bd" @click="setAddress(item)">
						<view class="name">
							<text>{{item.receiveName}}</text>
							<text>{{item.receivePhone}}</text>
						</view>
						<view class="p">
							{{item.provinceName}}/{{item.cityName}}/{{item.districtName}} {{item.address}}
						</view>
					</view>
					<view class="uni-inline-item">
						<view class="uni-flex-item">
							<label class="radio">
								<radio :value="item.addressId" :checked="item.isDefault == current" color="#06A972" style="transform:scale(0.8)" /> 设为默认
							</label>
						</view>
						<view class="btn" @click.stop="delAddress(item.addressId)">
							<image mode="widthFix" src="@/static/images/del.png" class="pic"></image> 
							<text>删除</text>
						</view>
						<view class="btn" @click.stop="saveAddress(item.addressId)">
							<image mode="widthFix" src="@/static/images/bianji.png" class="pic"></image> 
							<text>编辑</text>
						</view>
					</view>
				</view>
			</radio-group>
		</view>
		<view v-else>
			<u-empty mode="favor" icon="../../../../static/images/comment.png" text="暂无地址" width="320" height="320"
				textSize="24"></u-empty>
		</view>
		<view class="footer">
			<u-button type="primary" color="#06A972" text="添加地址" @click="saveAddress(0)"></u-button>
		</view>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				current: 'Y',
				addressList: [],
				
			}

		},

		onLoad() {
		},

		onShow() {
			let that = this;
			that.getAddressList();
		},

		methods: {
			
			/**
			 * 获取地址列表
			 */
			getAddressList() {
				let that = this;
				that.$request.get('userAddr/getList', {
					userInfoId: that.$store.state.app.uid
				}).then(res => {
					that.addressList = res.value;
				}).catch(err => {
					that.$util.Tips({
						title: err
					})
				})
			},
			
			/**
			 * 添加地址/修改地址
			 */
			saveAddress(addressId) {
				this.$util.Tips('/pages/goods/saveAddress/index?addressId=' + addressId)
			},
			
			/**
			 * 删除地址 
			 */
			delAddress(addressId) {
				let that = this;
				that.$request.get('userAddr/del', {
					addressId: addressId
				}).then(res => {
					that.$util.Tips({
						title: '删除成功'
					})
					that.addressList.forEach((e, i) => {
						if (e.addressId == addressId) {
							that.$delete(that.addressList, i);
						}
					})
				}).catch(err => {
					that.$util.Tips({
						title: err.msg
					})
				})
			},
			
			radioChange: function(e) {
				console.log(e.detail.value);
				let that = this;
				that.$request.get('userAddr/setDefault', {
					addressId: e.detail.value,
					isDefault: 'Y'
				}).then(res => {
					that.$util.Tips({
						title: '设置成功'
					})
				}).catch(err => {
					that.$util.Tips({
						title: err.msg
					})
				})
			},
			
			setAddress(item){
				let that = this;
				uni.$emit('addressId', item.addressId)
				uni.navigateBack();
			},
		}
	}
</script>

<style>
	page {
		background: #F3F3F3;
	}

	.container {
		padding: 20rpx;
	}

	.panel {
		background: #FFFFFF;
		border-radius: 14rpx;
		margin-bottom: 20rpx;
	}

	.panel .bd {
		padding: 20rpx 30rpx;
		border-bottom: 1px solid #eee;
	}

	.panel .p {
		margin-top: 15rpx;
		color: #666;
	}

	.panel .name {
		font-weight: bold;
	}

	.panel .name text {
		padding-right: 20rpx;
	}

	.panel .uni-inline-item {
		padding: 20rpx 30rpx;
		font-size: 24rpx;
	}

	.panel .uni-inline-item image {
		width: 18rpx;
		margin-right: 10rpx;
		height: auto;
	}

	.panel .uni-inline-item .btn {
		margin-left: 30rpx;
	}
</style>
